//
//  StringViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class StringViewController: CommonViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //MARK: - 字符串的遍历
    func demo15()
    {
        var string = "我要飞的风光"
        for i in string.characters
        {
            print(i)
        }
    }
    
    //MARK: - 字符串长度
    func demo16()
    {
        let str = "hello world您好"
        //返回指定编码对应的字节数量 lengthOfBytes
        //utf8中，每个汉字三个字节
        let length = str.lengthOfBytes(using: .utf8)
        print("字节个数 \(length)")
        
        //返回字符的个数
        print("字符个数 \(str.characters.count)")
        
        //类型转换 as
        let temstring = str as NSString
        print("长度 \(temstring.length)")
        
    }
    
    //MARK: - 字符串子串
    func demo17()
    {
        let str = "今天诗歌好天气"
        let tempString = str as NSString
        let subString1 = tempString.substring(to: 3)
        let subString2 = tempString.substring(from: 4)
        let subString3 = tempString.substring(with: NSMakeRange(2, 5))
        print("subString1= \(subString1)")
        print("subString2= \(subString2)")
        print("subString3= \(subString3)")
        
        //startIndex: _position 0
        print(str.startIndex)
        //endIndex: _position 字符串长度
        print(str.endIndex)
        
        let str1 = str.substring(from: "你好".endIndex) //诗歌好天气
        let str2 = str.substring(from: "342".startIndex) //今天诗歌好天气
        
        let str3 = str.substring(from: "你好".endIndex) //诗歌好天气
        let str4 = str.substring(from: "342".startIndex) //今天诗歌好天气
        print(str1)
        print(str2)
        print(str3)
        print(str4)
        
        print("-----")
        
        let str5 = str.range(of: "诗歌")
        print("str5= \(str5)")
        
        guard let str6 = str.range(of: "诗歌") else
        {
            print("一定没有找到")
            return
        }
        print("找到了")
        
        let str7 = str.substring(with: str6)
        print("str7= \(str7)")
        
        
        let str8 = str as NSString
        let str9 = str8.substring(with: NSMakeRange(0, 4))
        print("str9= \(str9)")
        
        
        // MARK - 字符串的拼接
        let name = "xiao ming"
        let age = 20
        let title: String? = "boss "
        let newString = "\(name) \(age)"
        print("newString= \(newString)")
        
        let string2 = "\(name) \(age) \(title)"
        print("string2= \(string2)")
        
        let string3 = "\(name) \(age) \(title ?? "")"
        print("string3= \(string3)")
        
        let point = CGPoint(x: 100, y: 30)
        let string4 = "\(name) \(age) \(title ?? "") \(point)"
        print("string4= \(string4)")
        
        //MARK - 字符串格式化
        let h = 12
        let m = 9
        let s = 11
        let time = "\(h) : \(m) : \(s)"
        print("time= \(time)")
        let date = String(format: "%02d:%02d:%02d",h,m,s)
        print("date= \(date)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
